package btb.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

import com.esotericsoftware.minlog.Log;
import com.esotericsoftware.minlog.Log.Logger;

public class CLogger extends Logger
{
	private static long beginLogTime = new Date().getTime();
	
	private static boolean toFile = false;
	
	private static String fileName = "default_log.txt";
	private static String filePath = "";
	
	@Override
	public void log(int _logLevel, String _category, String _message, Throwable _exception) {
		StringBuilder builder = new StringBuilder(256);
		
		long currentLogTime = new Date().getTime() - beginLogTime;
		
		if(currentLogTime == 0) {
			print("");
			print("---BEGIN LOG (" + new Date() + ")---");
			print("");
		}
		
		long days = currentLogTime/(1000*60*60*24);
		long hours = currentLogTime/(1000*60*24);
		long minutes = currentLogTime/(1000*60);
		long seconds = currentLogTime/(1000) % 60;
		
		if(days <= 9)
			builder.append("0");
		builder.append(days);
		
		builder.append(":");
		
		if(hours <= 9)
			builder.append("0");
		builder.append(hours);
		
		builder.append(":");
		
		if(minutes <= 9)
			builder.append("0");
		builder.append(minutes);
		
		builder.append(":");
		
		if(seconds <= 9)
			builder.append("0");
		builder.append(seconds);
		
		switch(_logLevel) {
			case Log.LEVEL_ERROR: builder.append(" ERROR:"); break;
			case Log.LEVEL_WARN: builder.append(" WARN:"); break;
			case Log.LEVEL_INFO: builder.append(" INFO:"); break;
			case Log.LEVEL_DEBUG: builder.append(" DEBUG:"); break;
			case Log.LEVEL_TRACE: builder.append(" TRACE:"); break;
		}
		
		if(_category != null) {
			builder.append("[");
			builder.append(_category);
			builder.append("] ");
		}
		
		builder.append(_message);
		
		if(_exception != null) {
			StringWriter writer = new StringWriter(256);
			_exception.printStackTrace(new PrintWriter(writer));
			builder.append("\n");
			builder.append(writer.toString().trim());
		}
		
		print(builder.toString());
	}
	
	protected void print(String _message) {
		if(CLogger.toFile) {
			PrintWriter fileOutput = null;
			
			try {
				fileOutput = new PrintWriter(new BufferedWriter(new FileWriter(CLogger.filePath + CLogger.fileName, true)));
				fileOutput.println(_message);
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} finally {
				fileOutput.close();
			}
		}
		
		System.out.println(_message);
	}
	
	public void toFile(boolean _toFile) {
		CLogger.toFile = _toFile;
	}
	
	public static void cond_info(String _head, String _tail){if(Log.INFO) Log.info(_head, _tail);}
	public static void cond_trace(String _head, String _tail){if(Log.TRACE) Log.trace(_head, _tail);}
	public static void cond_debug(String _head, String _tail){if(Log.DEBUG) Log.debug(_head, _tail);}
	public static void cond_warn(String _head, String _tail){if(Log.WARN) Log.warn(_head, _tail);}
	public static void cond_error(String _head, String _tail){if(Log.ERROR) Log.error(_head, _tail);}
}
